/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StudentFeesDetail;

import FeeDeposit.FeeDepositBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author aamod
 */
public class AddStudentFees extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session=request.getSession();
        try {
               String hfeeid[]=request.getParameter("hfeeid").split("/");
               float paidamt=Float.parseFloat(request.getParameter("pamnt"));
               float balamt=Float.parseFloat(request.getParameter("paid"));
               String challan=request.getParameter("challan");
               String cheque=request.getParameter("cheque");
               String bank=request.getParameter("bank");
               String branch=request.getParameter("branch");
               String sess=request.getParameter("sess");
               int pmode=Integer.parseInt(request.getParameter("pmode"));
               int adno=Integer.parseInt(request.getParameter("adno"));
               int semester=Integer.parseInt(request.getParameter("semester"));
               int feetype=Integer.parseInt(request.getParameter("feetype"));
               int catid=Integer.parseInt(request.getParameter("catid"));
               FeeDepositBean fd=new FeeDepositBean();
               fd.setAdmissionId(adno);
               fd.setSemester(semester);
               fd.setStudSession(sess);
               fd.setFeeTypeId(feetype);
               fd.setAmount(paidamt);
               fd.setPayMode(pmode);
               fd.setChequeNo(cheque);
               fd.setBank(bank);
               fd.setBranch(branch);
               fd.setUserId(Integer.parseInt(session.getAttribute("uid").toString()));
               fd.setReceiptNo(challan);
               Vector<Object> list=new Vector<Object>();
               float tempamt=paidamt+balamt;
               for(int i=0;i<hfeeid.length;i++)
                 {
                     String a[]=request.getParameterValues(hfeeid[i]+"-c");
                     if(a!=null)
                     {
                         StudentFeeDetailsBean st=new StudentFeeDetailsBean();
                         st.setSubFeeTypeId(Integer.parseInt(hfeeid[i]));
                         st.setAmount(Float.parseFloat(request.getParameter(hfeeid[i]+"-a")));
                         st.setConcessionAmount(Float.parseFloat(request.getParameter(hfeeid[i]+"-r")));
                         st.setAdditionalAmount(Float.parseFloat(request.getParameter(hfeeid[i]+"-add")));
                         st.setTotalAmount(Float.parseFloat(request.getParameter(hfeeid[i]+"-p")));
                         st.setAdmissionId(adno);
                         st.setSemester(semester);
                         st.setCategoryId(catid);
                         st.setUserId(Integer.parseInt(session.getAttribute("uid").toString()));
                         st.setFeeTypeId(feetype);
                         st.setUniqueFeeDetail(st.getSubFeeTypeId()+"--"+st.getFeeTypeId()+"--"+st.getAdmissionId()+"--"+st.getSemester()+"--"+st.getCategoryId());
                         if(st.getTotalAmount()<=tempamt)
                         {
                             st.setAmountPaid(st.getTotalAmount());
                             tempamt-=st.getTotalAmount();
                         }
                         else
                         {
                            st.setAmountPaid(tempamt);
                            tempamt-=tempamt;
                         }
                         list.add(st);
                     }
                  }
               int stfeedep=FeeDeposit.Query.add(fd);
               if(stfeedep!=0)
               {   
                   StudentFeesDetail.Query.delete("--"+feetype+"--"+adno+"--"+semester+"--"+catid);
                   stfeedep=StudentFeesDetail.Query.add(list);
                   if(stfeedep!=0)
                   {
                   session.setAttribute("status", "Success");
                   session.setAttribute("msg", "Added Successfully.");
                   response.sendRedirect("/RIT/Services/Accounts/FeeDeposit.jsp");
                   }
                   else
                   {
                   session.setAttribute("status", "Fail");
                   session.setAttribute("msg", "Student Fees Not Added. Please Try Again Later");
                   response.sendRedirect("/RIT/Services/Accounts/FeeDeposit.jsp");
                   }
               }
               else
               {
                   session.setAttribute("status", "Fail");
                   session.setAttribute("msg", "Student Fees Not Added. Please Try Again Later");
                   response.sendRedirect("/RIT/Services/Accounts/FeeDeposit.jsp");
               }
        }
        catch(Exception e)
        {
                   session.setAttribute("status", "Fail");
                   session.setAttribute("msg", "Exception Occured. Please Fill the Form Correctly");
                   response.sendRedirect("/RIT/Services/Accounts/FeeDeposit.jsp");
        }
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AddStudentFees.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AddStudentFees.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
